home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / dev / lang / SML.lha / sml / Programs / functions chap 15.3-15.4 < prev    next >
Encoding:
Text File  |  1991-12-12  |  1.2 KB  |  40 lines

  1. -fun map f [] = []
  2.   | map f (a::x) = (f a) :: (map f x)
  3. >val map = fn : ('a -> 'b) -> 'a list -> 'b list
  4.     (* the function map takes a function and a list as argument and *)
  5.     (* applies the function to all elements in the list.        *)
  6.     (* ie : -map square[1,3,5];  >[1,9,25]                *)
  7.  
  8. -fun zip f [] = []
  9.   | zip f (a::x) (b::y) = (f a b) :: (zip f x y);
  10. >val zip = fn : ?
  11.     (* this function zip takes a function and applies it on the    *)
  12.     (* elements in two list. the elements have the same place in     *)
  13.     (* the list.                            *)
  14.  
  15.  
  16.  
  17. -fun twice f = f o f;
  18. >val twice = fn : ('a -> 'a) -> 'a -> 'a
  19.     (* the compose operator 'o' is a standard in ML and has lower    *)
  20.     (* precedence than any other standard operator            *)
  21.     (* ie : twice sqr 5 ->(sqr o sqr)5 ->sqr(sqr 5) ->sqr 25 ->625    *)
  22.  
  23.     -fun last = hd o rev;
  24.     >val last = fn : 'a list -> 'a
  25.     
  26.     -fun second = hd o tl;
  27.     >val second = fn : 'a list -> 'a
  28.  
  29.     -fun third = hd o tl o tl;
  30.     >val third = fn : 'a list -> 'a
  31.         (* ie : third[1,3,5] -> (hd o tl o tl)[1,3,5] ->     *)
  32.         (*      (hd o tl)(tl[1,3,5]) -> (hd o tl)[3,5] ->    *)
  33.         (*      hd(tl[3,5]) -> hd[5] -> 5            *)
  34.     
  35.     -fun compose f g x = f (g x);
  36.  
  37.     -fun f x = g(h x);
  38.     -fun f x = (g o h)x;
  39.     -val f = g o h;            
  40.